<template>
  <view class="template-index tn-safe-area-inset-bottom">
    <!-- 顶部导航栏 -->
    <tn-nav-bar fixed alpha :isBack="false">
      <view class="nav-content">
        <view class="search-box" @click="tn('/resourcePages/search/search')">
          <text class="tn-icon-search"></text>
          <text class="placeholder-text">搜索更多资源...</text>
        </view>
      </view>
    </tn-nav-bar>
    
    <view class="page-content" :style="{paddingTop: vuex_custom_bar_height + 'px'}">
      
      <!-- 轮播图区域 -->
      <view class="swiper-container">
        <swiper class="card-swiper" @click="tn('/homePages/navigation')" :circular="true"
          :autoplay="true" duration="500" interval="8000" @change="cardSwiper"> 
          <swiper-item v-for="(item,index) in bannerList" :key="index" :class="cardCur==index?'cur':''">
            <view class="swiper-item">
              <image :src="item.banner_image" mode="aspectFill" v-if="item.type=='image'"></image>
            </view>
          </swiper-item>
        </swiper>
        <view class="indication">
          <block v-for="(item,index) in bannerList" :key="index">
            <view class="spot" :class="cardCur==index?'active':''"></view>
          </block>
        </view>
      </view>
      
      <!-- 功能导航区 -->
      <view class="function-grid">
        <view class="function-item" @click="tn('/resourcePages/search/search')">
          <text class="function-icon tn-icon-search"></text>
          <view class="function-content">
            <text class="function-title">资源搜索</text>
            <text class="function-desc">海量资源等你发现</text>
          </view>
          <text class="function-arrow tn-icon-right"></text>
        </view>
        <view class="function-item" @click="tn('/resourcePages/wallpaper/wallpaper')">
          <text class="function-icon tn-icon-image"></text>
          <view class="function-content">
            <text class="function-title">壁纸资源</text>
            <text class="function-desc">精美壁纸任你选择</text>
          </view>
          <text class="function-arrow tn-icon-right"></text>
        </view>
      </view>
      
      <!-- 热门智能体区域 -->
      <view class="section">
        <view class="section-header">
          <view class="section-title">热门智能体</view>
          <view class="section-more tn-icon-right" @click="tn('/activityPages/project')"></view>
        </view>
        
        <view class="bot-grid">
          <block v-for="(item,index) in bots" :key="index">
            <view class="bot-card" @click="jumpBotManager(item)">
              <view class="bot-image">
                <image class="tn-width-full tn-height-full" :src="item.bg_image" mode="aspectFill"></image>
              </view>
              <view class="bot-info">
                <view class="bot-name">{{ item.name }}</view>
                <view class="bot-stats">
                  <text class="bot-stat-item">
                    <text class="bot-stat-icon tn-icon-star"></text>
                    <text class="bot-stat-value">4.9</text>
                  </text>
                  <text class="bot-stat-item">
                    <text class="bot-stat-icon tn-icon-message"></text>
                    <text class="bot-stat-value">999+</text>
                  </text>
                </view>
              </view>
            </view>
          </block>
        </view>
      </view>
    </view>

    <view class='tn-tabbar-height'></view>
  </view>
</template>

<script>
	import { getBotPage } from '@/api/bot.js'
	import { getBannerPage } from '@/api/home.js'
  export default {
    name: 'Index',
    data(){
      return {
        cardCur: 0,
        isAndroid: true,
        bannerList: [],
        cardCur2: 0,
				bots: [],
				pageInfo: {
					page: 1,
					limit: 10
				}
      }
    },
    created() {
      const systemInfo = uni.getSystemInfoSync()
      if (systemInfo.system.toLocaleLowerCase().includes('ios')) {
        this.isAndroid = false
      } else {
        this.isAndroid = true
      }
    },
		onReady() {
			this.getBotPageInterface()
			this.getBannerPageInterface()
		},
    methods: {
			getBotPageInterface() {
				return new Promise((resolve, reject) => {
					const params = {
						page: this.pageInfo.page,
						limit: this.pageInfo.limit
					}
					getBotPage(params).then(res => {
						if(res && res.success) {
							this.bots = res.data.records || []
						}
					})
				})
			},
			getBannerPageInterface() {
				return new Promise((resolve, reject) => {
					const params = {
						page: 1,
						limit: 5
					}
					getBannerPage(params).then(res => {
						if(res && res.success) {
							this.bannerList = res.data.records || []
						}
					})
				})
			},
      // cardSwiper
      cardSwiper(e) {
        this.cardCur = e.detail.current
      },
      // resume
      resume(e) {
        this.cardCur2 = e.detail.current
      },
      // 跳转
      tn(e) {
      	uni.navigateTo({
      		url: e,
      	});
      },
			// 跳转到详情的智能体
			jumpBotManager(bot) {
				uni.navigateTo({
					url: `/pages/botTranstion/botTranstion?id=${bot.id}`,
				});
			},
			navigatorTo(e) {
				uni.navigateTo({
					url: e,
				});
			}
    }
  }
</script>

<style lang="scss" scoped>
  /* 颜色变量 */
  :root {
    --primary-color: #3E64FF;
    --secondary-color: #5EDFFF;
    --accent-color: #00C9A7;
    --dark-bg: #1E1E2E;
    --light-bg: #F8FAFF;
    --text-color: #2B2D42;
    --text-secondary: #8D99AE;
    --card-bg: #FFFFFF;
    --card-shadow: 0 6rpx 16rpx rgba(13, 38, 76, 0.06);
    --gradient-blue: linear-gradient(135deg, #3E64FF 0%, #5EDFFF 100%);
    --gradient-green: linear-gradient(135deg, #00C9A7 0%, #5EDFFF 100%);
  }

  /* 基础样式 */
  .template-index {
    background-color: var(--light-bg);
    max-height: 100vh;
    padding-bottom: 100rpx;
    position: relative;
  }

  /* 导航栏内容样式 */
  .nav-content {
    width: 75%;
    height: 100%;
    padding: 0 30rpx;
    position: relative;
    
    .search-box {
      width: 100%;
      height: 64rpx;
      background-color: rgba(255, 255, 255, 0.9);
      border-radius: 32rpx;
      display: flex;
      align-items: center;
      padding: 0 30rpx;
      
      .tn-icon-search {
        font-size: 32rpx;
        color: var(--text-secondary);
        margin-right: 10rpx;
      }
      
      .placeholder-text {
        font-size: 28rpx;
        color: var(--text-secondary);
      }
    }
  }

  /* 页面内容区域 */
  .page-content {
    padding-top: 100rpx;
  }

  /* 轮播图容器 */
  .swiper-container {
    position: relative;
    margin: 0 30rpx 30rpx;
    border-radius: 20rpx;
    overflow: hidden;
    box-shadow: var(--card-shadow);
  }
  
  .card-swiper {
    height: 340rpx !important;
    border-radius: 20rpx;
    overflow: hidden;
    
    .swiper-item {
      width: 100%;
      height: 100%;
      border-radius: 20rpx;
      position: relative;
      overflow: hidden;
      
      image {
        width: 100%;
        height: 100%;
        border-radius: 20rpx;
      }
    }
  }

  /* 指示点样式 */
  .indication {
    position: absolute;
    bottom: 20rpx;
    right: 20rpx;
    display: flex;
    z-index: 10;
    
    .spot {
      width: 12rpx;
      height: 12rpx;
      background: rgba(255, 255, 255, 0.5);
      border-radius: 6rpx;
      margin: 0 6rpx;
      transition: all 0.3s ease;
      
      &.active {
        width: 30rpx;
        background: white;
      }
    }
  }

  /* 功能导航网格 */
  .function-grid {
    margin: 20rpx 30rpx;
    display: grid;
    grid-template-columns: repeat(2, 1fr);
    gap: 20rpx;
    
    .function-item {
      background: linear-gradient(135deg, rgba(255,255,255,0.95) 0%, #FFFFFF 100%);
      border-radius: 16rpx;
      padding: 20rpx;
      display: flex;
      align-items: center;
      box-shadow: 0 8rpx 20rpx rgba(62, 100, 255, 0.08);
      transition: all 0.3s ease;
      position: relative;
      overflow: hidden;
      
      &::before {
        content: '';
        position: absolute;
        top: 0;
        left: 0;
        right: 0;
        height: 2rpx;
        background: linear-gradient(90deg, var(--primary-color) 0%, var(--secondary-color) 100%);
        opacity: 0.8;
      }
      
      .function-icon {
        font-size: 36rpx;
        color: var(--primary-color);
        margin-right: 16rpx;
        background: rgba(62, 100, 255, 0.1);
        width: 70rpx;
        height: 70rpx;
        border-radius: 35rpx;
        display: flex;
        align-items: center;
        justify-content: center;
        transition: all 0.3s ease;
      }
      
      .function-content {
        flex: 1;
        
        .function-title {
          font-size: 28rpx;
          font-weight: bold;
          color: var(--text-color);
          margin-bottom: 4rpx;
          display: block;
        }
        
        .function-desc {
          font-size: 22rpx;
          color: var(--text-secondary);
          display: block;
          white-space: nowrap;
          overflow: hidden;
          text-overflow: ellipsis;
        }
      }
      
      .function-arrow {
        color: var(--text-secondary);
        font-size: 28rpx;
        opacity: 0.8;
      }
      
      &:active {
        transform: scale(0.96);
        box-shadow: 0 4rpx 12rpx rgba(62, 100, 255, 0.05);
        
        .function-icon {
          transform: scale(0.9);
          background: rgba(62, 100, 255, 0.15);
        }
      }
    }
  }

  /* 区域标题样式 */
  .section {
    margin: 40rpx 30rpx 20rpx;
    
    .section-header {
      display: flex;
      justify-content: space-between;
      align-items: center;
      margin-bottom: 24rpx;
      
      .section-title {
        font-size: 32rpx;
        font-weight: bold;
        color: var(--text-color);
        position: relative;
        padding-left: 20rpx;
        
        &::before {
          content: '';
          position: absolute;
          width: 6rpx;
          height: 30rpx;
          background: var(--primary-color);
          left: 0;
          top: 50%;
          transform: translateY(-50%);
          border-radius: 3rpx;
        }
      }
      
      .section-more {
        color: var(--text-secondary);
        font-size: 28rpx;
      }
    }
  }

  /* 智能体卡片网格 */
  .bot-grid {
    display: grid;
    grid-template-columns: repeat(2, 1fr);
    gap: 30rpx;
    padding: 10rpx 4rpx 20rpx;
    
    .bot-card {
      position: relative;
      background: #FFFFFF;
      border-radius: 24rpx;
      overflow: hidden;
      box-shadow: 
        0 10rpx 30rpx -5rpx rgba(62, 100, 255, 0.15),
        0 4rpx 10rpx -6rpx rgba(0, 0, 0, 0.1);
      transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
      transform: translateY(0);
      
      &::after {
        content: '';
        position: absolute;
        top: 0;
        left: 0;
        right: 0;
        height: 2rpx;
        background: linear-gradient(90deg, 
          var(--primary-color) 0%,
          var(--secondary-color) 100%
        );
        opacity: 0.8;
      }
      
      &:active {
        transform: translateY(6rpx);
        box-shadow: 
          0 5rpx 15rpx -5rpx rgba(62, 100, 255, 0.1),
          0 2rpx 6rpx -6rpx rgba(0, 0, 0, 0.06);
      }
      
      .bot-image {
        height: 260rpx;
        position: relative;
        overflow: hidden;
        
        &::after {
          content: '';
          position: absolute;
          left: 0;
          right: 0;
          bottom: 0;
          height: 70%;
          background: linear-gradient(
            to bottom,
            rgba(0, 0, 0, 0) 0%,
            rgba(0, 0, 0, 0.4) 100%
          );
          z-index: 1;
        }
        
        image {
          width: 100%;
          height: 100%;
          object-fit: cover;
          transform: scale(1.02);
          transition: transform 0.6s cubic-bezier(0.4, 0, 0.2, 1);
        }
      }
      
      &:hover .bot-image image {
        transform: scale(1.06);
      }
      
      .bot-info {
        padding: 24rpx;
        position: relative;
        background: linear-gradient(
          to bottom,
          rgba(255, 255, 255, 0.95) 0%,
          rgba(255, 255, 255, 1) 100%
        );
        
        .bot-name {
          font-size: 32rpx;
          font-weight: 600;
          color: var(--text-color);
          margin-bottom: 20rpx;
          overflow: hidden;
          text-overflow: ellipsis;
          white-space: nowrap;
          letter-spacing: 0.5rpx;
        }
        
        .bot-stats {
          display: flex;
          gap: 20rpx;
          
          .bot-stat-item {
            display: flex;
            align-items: center;
            padding: 5rpx 10rpx;
            border-radius: 30rpx;
            backdrop-filter: blur(5px);
            border: 1px solid rgba(255, 255, 255, 0.1);
            
            .bot-stat-icon {
              font-size: 26rpx;
              color: var(--primary-color);
              margin-right: 8rpx;
            }
            
            .bot-stat-value {
              font-size: 26rpx;
              color: var(--primary-color);
              font-weight: 500;
              letter-spacing: 0.5rpx;
            }
          }
        }
      }
    }
  }

  /* 底部间距 */
  .tn-tabbar-height {
    min-height: 100rpx;
    height: calc(200rpx + env(safe-area-inset-bottom) / 2);
  }
</style>